﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Hotel_application.Models;
using System.Data.Entity.Validation;
using System.Diagnostics;

namespace Hotel_application.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            var ks = new KhachSanEntities();
            return View(ks);
        }

        public ActionResult QLTKCongTy()
        {
            var ks = new KhachSanEntities();
            return View(ks);
        }

        public ActionResult QLTKNhanVien()
        {
            var ks = new KhachSanEntities();            
            return View(ks);
        }

        [HttpGet]
        public ActionResult TaoTKCongTy()
        {
            var ks = new KhachSanEntities();
            string max_maTK = ks.CongTies.Select(e => e.maTK).OrderByDescending(p => p).FirstOrDefault();
            max_maTK = max_maTK.Substring(2);
            int max_maTK_int = Convert.ToInt32(max_maTK);
            max_maTK_int += 1;
            max_maTK = Convert.ToString(max_maTK_int).PadLeft(6, '0');
            string maTK = "CT" + max_maTK;

            TaiKhoan tk = new TaiKhoan();
            tk.maTK = maTK;
            tk.passwordTK = Request["passwordTK"];
            tk.email = Request["email"];
            ks.TaiKhoans.Add(tk);
            ks.SaveChanges();

            string max_maCT = ks.CongTies.Select(e => e.maCT).OrderByDescending(p => p).FirstOrDefault();
            max_maCT = max_maCT.Substring(2);
            int max_maCT_int = Convert.ToInt32(max_maCT);
            max_maCT_int += 1;
            max_maCT = Convert.ToString(max_maCT_int).PadLeft(6, '0');
            string maCT = "CT" + max_maCT;

            CongTy ct = new CongTy();
            ct.maCT = maCT;
            ct.maTK = maTK;
            ct.tenCT = Request["tenCT"];
            ct.tenTKNH = Request["tenTKNH"];
            ct.maTKNH = Request["maTKNH"];
            ct.diaChi = Request["diaChi"];
            ct.soDT = Request["soDT"];
            ks.CongTies.Add(ct);
            ks.SaveChanges();

            
            return Content("Tạo tài khoản thành công");
        }

        [HttpGet]
        public ActionResult CapNhatTKCongTy()
        {
            var ks = new KhachSanEntities();
            string id = Request["maCT"];

            CongTy ct = ks.CongTies.First(i => i.maCT == id);
            TaiKhoan tk = ks.TaiKhoans.First(j => j.maTK == ct.maTK);            
           
            tk.passwordTK = Request["passwordTK"];
            tk.email = Request["email"];            
            ks.SaveChanges();

            ct.tenCT = Request["tenCT"];
            ct.tenTKNH = Request["tenTKNH"];
            ct.maTKNH = Request["maTKNH"];
            ct.diaChi = Request["diaChi"];
            ct.soDT = Request["soDT"];            
            ks.SaveChanges();


            return Content("Cập nhật tài khoản thành công");
        }

        [HttpGet]
        public ActionResult Delete_TK()
        {            
            var ks = new KhachSanEntities();
            string id = Request.QueryString["maCT"];
            
            CongTy ct = ks.CongTies.First(i => i.maCT == id);
            TaiKhoan tk = ks.TaiKhoans.First(j => j.maTK == ct.maTK);

            ks.CongTies.Remove(ct);
            ks.TaiKhoans.Remove(tk);
            ks.SaveChanges();
            return Content("Đã xóa tài khoản thành công");      

        }

        [HttpGet]
        public ActionResult TaoTKNhanVien()
        {
            var ks = new KhachSanEntities();
            
            string max_maTK = ks.NhanViens.Select(e => e.maTK).OrderByDescending(p => p).FirstOrDefault();
            max_maTK = max_maTK.Substring(2);
            int max_maTK_int = Convert.ToInt32(max_maTK);
            max_maTK_int += 1;
            max_maTK = Convert.ToString(max_maTK_int).PadLeft(6, '0');
            string maTK = "NV" + max_maTK;

            string max_maNV = ks.NhanViens.Select(e => e.maNV).OrderByDescending(p => p).FirstOrDefault();
            max_maNV = max_maNV.Substring(2);
            int max_maNV_int = Convert.ToInt32(max_maNV);
            max_maNV_int += 1;
            max_maNV = Convert.ToString(max_maNV_int).PadLeft(8, '0');
            string maNV = "NV" + max_maNV;
            try
            {
                TaiKhoan tk = new TaiKhoan();
                tk.maTK = maTK;
                tk.passwordTK = Request["passwordTK"];
                tk.email = Request["email"];
                ks.TaiKhoans.Add(tk);
                ks.SaveChanges();

                NhanVien nv = new NhanVien();
                nv.maNV = maNV;
                nv.tenNV = Request["tenNV"];
                nv.maLoai = Request["maLoai"];
                nv.maTK = maTK;
                ks.NhanViens.Add(nv);
                ks.SaveChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                    }
                }
            }

            return Content("Tạo tài khoản thành công");
        }



        [HttpGet]
        public ActionResult Delete_TKNV()
        {
            var ks = new KhachSanEntities();
            string id = Request.QueryString["maTK"];
                        
            TaiKhoan tk = ks.TaiKhoans.First(j => j.maTK == id);
            NhanVien nv = ks.NhanViens.First(i => i.maTK == id);

            ks.NhanViens.Remove(nv);
            ks.TaiKhoans.Remove(tk);
            ks.SaveChanges();
            return Content("Đã xóa tài khoản thành công");

        }

        [HttpGet]
        public ActionResult CapNhatTKNhanVien()
        {
            var ks = new KhachSanEntities();
            var maTK = Request["maTK"];
            try
            {
                TaiKhoan tk = ks.TaiKhoans.First(j => j.maTK == maTK);
                tk.passwordTK = Request["passwordTK"];
                tk.email = Request["email"];                
                ks.SaveChanges();

                NhanVien nv = ks.NhanViens.First(i => i.maTK == maTK);   
                nv.tenNV = Request["tenNV"];
                nv.maLoai = Request["maLoai"];    
                
                ks.SaveChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                    }
                }
            }

            return Content("Cập nhật tài khoản thành công");
        }

    }
}
